$(function () {
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();

    /**
     * 页面初始化
     */
    function init() {
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent() {
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function () {
            render(true, 1, pageSize);
            return false;
        });

        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function (event) {
            event = event ? event : window.event;
            if (event.keyCode == 13) {
                render(true, 1, pageSize);
                return false;
            }
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo() {
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function (i, field) {
            selectInfo[field.name] = field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }

    /**
     * 分页显示方法
     */
    function myPagination() {
        render(true, 1, pageSize);
        //调用公共分页方法
        pagination("#page-selection", {pageSize: pageSize, total: total}, render);

    }

    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async, pageIndex, pageSize) {
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async: async,
            type: 'post',
            url: 'pagingVideoAuthPackage',
            data: selectInfo,//从1开始计数
            dataType: 'json',
            success: function (result) {
                var html = '';
                if (result.errorCode == 0) {
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total: total, total_count: total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html += '<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="">套餐名称</th>' +
                        '<th class="">套餐金额</th>' +
                        '<th class="">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for (var i = 0; i < myList.length; i++) {
                        var obj = myList[i];
                        var num = (pageIndex - 1) * pageSize + i + 1;
                        var title = obj.title;
                        var money = obj.money;
                        var id = obj.id;
                        var checked = (idList.indexOf(id) >= 0) ? "checked" : "";//判断当前记录先前有没有被选中
                        html += '<tr>'
                            + '<td class="">' + num + '</td>'
                            + '<td class="">' + title + '</td>'
                            + '<td class="">' + money + '</td>'
                            + '<td>'
                            + '<a href="editVideoAuthPackage?id=' + id + '" data-id="' + id + '" class="btn btn-primary btn-xs edit-order">编辑</a>'
                            + '<a href="javascript:;" data-id="' + id + '" class="btn btn-default btn-xs deleteVideoAuthPackage">删除</a>'
                            + '</td>'
                            + '</tr>';
                    }

                    if (myList.length == 0) {
                        html += '<tr><td colspan="' + colspan + '"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    } else {
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList, ".inner-section #list-table .select-all", ".inner-section #list-table .select-single");

                        //删除
                        $(".deleteVideoAuthPackage").click(deleteVideoAuthPackage);
                    }

                } else {
                    responseTip(result.errorCode, result.errorInfo, 1500);
                }
            },
            error: errorResponse
        });
    }

    /**
     * 删除
     * @param aid
     * @param state
     */
    function deleteVideoAuthPackage() {
        var id = $(this).attr('data-id');
        myConfirmModal("确定删除吗？", function () {
            $.ajax(
                {
                    type: "post",
                    url: "deleteVideoAuthPackage",
                    data: {"id": id},
                    dataType: "json",
                    beforeSend: function (xhr) {
                        //显示“加载中。。。”
                        $("#loading").modal('show');
                    },
                    complete: function () {
                        //隐藏“加载中。。。”
                        $("#loading").modal('hide');
                    },
                    success: function (json, statusText) {
                        if (json.errorCode == 0) {
                            if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true, currentPage, pageSize);
                        } else {
                            responseTip(json.errorCode, json.errorInfo, 1500);
                        }
                    },
                    error: errorResponse
                }
            );
        });
    }

    init();
});